package tasknow.testdriver;

import static org.junit.Assert.*;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Calendar;
import java.util.Vector;

import org.junit.Test;

import tasknow.storage.InvalidFileNameException;
import tasknow.storage.Storage;
import tasknow.storage.DamagedXmlFileException;
import tasknow.utils.Task;

public class StorageTest {

	// @author A0118386X
	
	private BufferedReader br;

	// Basic testing --------------------------------------------

	@Test
	public void testRead() throws Exception {
		Storage storage = new Storage("testCase.xml");
		Vector<Task> taskVector = storage.readFromFile();

		assertEquals("testTask2", taskVector.elementAt(0).getDescription());
		assertEquals("15/03/12", taskVector.elementAt(0).getStartDateString());
		assertEquals("13:00", taskVector.elementAt(0).getStartTimeString());
		assertEquals("15/03/13", taskVector.elementAt(0).getEndDateString());
		assertEquals("13:00", taskVector.elementAt(0).getEndTimeString());
		assertEquals("Completed ", taskVector.elementAt(0)
				.getTaskStatusString());
	}

	@Test
	public void testWrite() throws Exception {
		Storage storage = new Storage("testWrite.xml");
		Vector<Task> taskVector = new Vector<Task>();
		Task task = createTask();

		taskVector.add(task);
		storage.saveToFile(taskVector);
		String expected = xmlToString("testCase.xml");
		String real = xmlToString("testWrite.xml");

		assertEquals(expected, real);
	}
	
	@Test
	public void testDamagedRead() throws InvalidFileNameException {
		Throwable caught = null;
		Storage storage = new Storage("damagedCase.xml");
		
		try {
			Vector<Task> taskVector = storage.readFromFile();
			taskVector.clear();
		} catch (DamagedXmlFileException e) {
			caught = e;
		}
		
		assertNotNull(caught);
		assertSame(DamagedXmlFileException.class, caught.getClass());
		assertEquals("Non-numerical value found.", caught.getMessage());
	}
	
	@Test
	public void testInvalidFileName() {
		Throwable caught = null;
		
		try {
			Storage storage = new Storage("fileName*.xml");
			storage.getFileName();
		} catch (InvalidFileNameException e) {
			caught = e;
		}
		
		assertNotNull(caught);
		assertSame(InvalidFileNameException.class, caught.getClass());
		assertEquals("No valid file name found.", caught.getMessage());
	}
	
	private Task createTask() {
		Task task = new Task();
		Calendar startCalendar = Calendar.getInstance();
		startCalendar.set(2012, 02, 15, 13, 00);
		Calendar endCalendar = Calendar.getInstance();
		endCalendar.set(2013, 02, 15, 13, 00);

		task.modifyDescription("testTask2");
		task.modifyStartCalendar(startCalendar);
		task.modifyEndCalendar(endCalendar);
		task.mark();

		return task;
	}

	private String xmlToString(String filename) throws FileNotFoundException,
	IOException {
		br = new BufferedReader(new FileReader(new File(filename)));
		String line;
		StringBuilder sb = new StringBuilder();

		while ((line = br.readLine()) != null) {
			sb.append(line.trim());
		}

		return sb.toString();
	}
	
}